import Cookies from 'js-cookie'

const TokenKey = 'Admin-Token'
const UserKey = 'Current-User'
const QRScanCode = 'QRScanCode'

// 设置过期时间（例如，2 天）
const expiresIn = 3

export const getToken = () => Cookies.get(TokenKey)

export const setToken = token => Cookies.set(TokenKey, token, { expires: expiresIn })

export const removeToken = () => Cookies.remove(TokenKey)

export const getUser = () => Cookies.get(UserKey)

export const setUser = user => Cookies.set(UserKey, user, { expires: expiresIn })

export const delUser = () => Cookies.remove(UserKey)

export const setQRScanCode = code => Cookies.set(QRScanCode, code, { expires: 1 / 144 })

export const getQRScanCode = () => Cookies.get(QRScanCode)

export const delQRScanCode = () => Cookies.remove(QRScanCode)



export const updateUser = updatedUser => {
  // 获取当前用户
  const currentUser = getUser()

  // 获取当前用户的 Cookie 信息
  const currentUserCookie = getUser(UserKey)

  // 计算剩余过期时间
  const remainingTime = (new Date(currentUserCookie.expires) - new Date()) / (1000 * 60 * 60 * 24)

  // 使用剩余过期时间更新用户
  Cookies.set(UserKey, updatedUser, { expires: remainingTime })
}
